package com.example.mywatermask;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import java.util.Random;

public class MainActivity2 extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private ImageView ivWaterMark;
    private RadioGroup mRadioGroup;

    private WaterMaskView waterMaskView;
    private Bitmap srcBitmap;
    private Bitmap waterBitmap;
    private Bitmap resultBitmap;

    private final static int LEFT_TOP = 0;
    private final static int RIGHT_TOP = 1;
    private final static int RIGHT_BOTTOM = 2;
    private final static int LEFT_BOTTOM = 3;
    private int position = 0;
    private Random mRandom = new Random();
    private RadioGroup mRadioGroup2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        ivWaterMark = findViewById(R.id.wartermark_pic);
        mRadioGroup = findViewById(R.id.rg_wartermark);
        mRadioGroup2 = findViewById(R.id.rg_wartermark2);
        mRadioGroup.setOnCheckedChangeListener(this);
        mRadioGroup2.setOnCheckedChangeListener(this);


        waterMaskView = new WaterMaskView(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        waterMaskView.setLayoutParams(params);
        waterMaskView.setCompanyText("XXXXXX公司");
        waterMaskView.setInfoText("这是相关信息1这是相关信息2这是相关信息3这是相关信息4这是相关信息5");

        //初始化两个图片
        srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
        waterBitmap = WaterMaskUtil.convertViewToBitmap(waterMaskView);

        //默认设置水印位置在左下
        saveWaterMask(LEFT_BOTTOM);
    }

    /**
     * @param position 左上为0，顺时针算起
     */
    private void saveWaterMask(int position) {
        waterBitmap = WaterMaskUtil.zoomBitmap(srcBitmap, waterBitmap);
        switch (position) {
            case LEFT_TOP:
                resultBitmap = WaterMaskUtil.createWaterMaskLeftTop(this, srcBitmap, waterBitmap, 0, 0);
                break;
            case RIGHT_TOP:
                resultBitmap = WaterMaskUtil.createWaterMaskRightTop(this, srcBitmap, waterBitmap, 0, 0);
                break;
            case RIGHT_BOTTOM:
                resultBitmap = WaterMaskUtil.createWaterMaskRightBottom(this, srcBitmap, waterBitmap, 0, 0);
                break;
            case LEFT_BOTTOM:
                resultBitmap = WaterMaskUtil.createWaterMaskLeftBottom(this, srcBitmap, waterBitmap, 0, 0);
                break;
        }
        ivWaterMark.setImageBitmap(resultBitmap);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        switch (i) {
            case R.id.rb_wartermark_lefttop:
                waterBitmap = WaterMaskUtil.convertViewToBitmap(waterMaskView);
                position = LEFT_TOP;
                saveWaterMask(position);
                break;
            case R.id.rb_wartermark_righttop:
                waterBitmap = WaterMaskUtil.convertViewToBitmap(waterMaskView);
                position = RIGHT_TOP;
                saveWaterMask(position);
                break;
            case R.id.rb_wartermark_rightbottom:
                waterBitmap = WaterMaskUtil.convertViewToBitmap(waterMaskView);
                position = RIGHT_BOTTOM;
                saveWaterMask(position);
                break;
            case R.id.rb_wartermark_leftbottom:
                waterBitmap = WaterMaskUtil.convertViewToBitmap(waterMaskView);
                position = LEFT_BOTTOM;
                saveWaterMask(position);
                break;

            case R.id.rb1:
                srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                waterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b);
                position = mRandom.nextInt(4);
                saveWaterMask(position);
                break;
            case R.id.rb2:
                srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b);
                waterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                position = mRandom.nextInt(4);
                saveWaterMask(position);
                break;
            case R.id.rb3:
                srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b);
                waterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.watermask);
                position = mRandom.nextInt(4);
                saveWaterMask(position);
                break;
            case R.id.rb4:
                srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                waterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.watermask);
                position = mRandom.nextInt(4);
                saveWaterMask(position);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (srcBitmap != null) {
            srcBitmap.recycle();
            srcBitmap = null;
        }
        if (waterBitmap != null) {
            waterBitmap.recycle();
            waterBitmap = null;
        }
    }

}